﻿using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Yglu.BusinessLayer;
using Yglu.BusinessLayer.Entities;
using Yglu.BusinessLayer.Enums;

namespace Yglu.TerminalApp
{
	public partial class PaymentForm : Form
	{
		#region Properties

		public Order CurrentOrder { get; set; }

		#endregion

		#region Main methods

		public PaymentForm(Order newOrder)
		{
			InitializeComponent();

			CurrentOrder = newOrder;
		}

		private void PaymentForm_Load(object sender, EventArgs e)
		{
			lblCustomer.Text = CurrentOrder.Combos.Count + " " + lblCustomer.Text + " " + CurrentOrder.Customer;
			txtValueCharged.Text = CurrentOrder.PriceWithDiscount.ToString("0.00");

			String promotions = String.Empty;

			foreach (Combo c in CurrentOrder.Combos)
			{
				if (c.PromotionApplied != null && !promotions.Contains(c.PromotionApplied.Title))
				{
					promotions += c.PromotionApplied.Title + ", ";
				}
			}

			if (CurrentOrder.PromotionApplied != null)
			{
				promotions += CurrentOrder.PromotionApplied.Title + ", ";
			}

			if (promotions.Length > 1)
			{
				lblDiscounts.Text = Messages.AppliedDiscounts + ": " + promotions.Substring(0, promotions.Length - 2);
			}
		}

		#endregion

		#region Confirm payment

		private void txtValueReceived_KeyDown(object sender, KeyEventArgs e)
		{
			if (!Program.InputIsNumber(e.KeyCode))
			{
				e.SuppressKeyPress = true;
				e.Handled = true;
			}

			if (txtValueReceived.Text.Length > 1)
			{
				String newValue = txtValueReceived.Text.Replace(" ", String.Empty);

				if (Thread.CurrentThread.CurrentUICulture.NumberFormat.CurrencyDecimalSeparator == ".")
				{
					newValue = newValue.Replace(",", ".");
				}
				else
				{
					newValue = newValue.Replace(".", ",");
				}

				txtValueReceived.Text = newValue;
			}
		}

		private void txtValueReceived_TextChanged(object sender, EventArgs e)
		{
			Double received = Convert.ToDouble(txtValueReceived.Text);
			Double charged = Convert.ToDouble(txtValueCharged.Text);

			txtValueChange.Text = (received - charged).ToString("0.00");
		}

		private void butPaymentCash_Click(Object sender, EventArgs e)
		{
			ProcessPayment(sender, PaymentType.Cash);
		}

		private void butPaymentVisa_Click(Object sender, EventArgs e)
		{
			ProcessPayment(sender, PaymentType.CreditCardVisa);
		}

		private void butPaymentMasterCard_Click(Object sender, EventArgs e)
		{
			ProcessPayment(sender, PaymentType.CreditCardMaster);
		}

		private void butPaymentAmex_Click(Object sender, EventArgs e)
		{
			ProcessPayment(sender, PaymentType.CreditCardAmex);
		}

		private void ProcessPayment(Object sender, PaymentType t)
		{
			CurrentOrder.PaymentType = t;

			butPaymentCash.ForeColor = SystemColors.ControlText;
			butPaymentVisa.ForeColor = SystemColors.ControlText;
			butPaymentMasterCard.ForeColor = SystemColors.ControlText;
			butPaymentAmex.ForeColor = SystemColors.ControlText;

			butPaymentCash.BackColor = SystemColors.Control;
			butPaymentVisa.BackColor = SystemColors.Control;
			butPaymentMasterCard.BackColor = SystemColors.Control;
			butPaymentAmex.BackColor = SystemColors.Control;

			Button but = (Button)sender;
			but.ForeColor = SystemColors.HighlightText;
			but.BackColor = SystemColors.Highlight;

			if (t == PaymentType.Cash || t == PaymentType.Cheque)
			{
				txtValueReceived.ReadOnly = false;
			}
			else
			{
				txtValueReceived.Text = txtValueCharged.Text;
				txtValueReceived.ReadOnly = true;
			}

			txtValueReceived.BackColor = SystemColors.Window;

			butConfirm.Visible = true;
			butCancel.Visible = true;
		}

		#endregion

		#region Complete order

		private void butConfirm_Click(object sender, EventArgs e)
		{
			CurrentOrder.DatePaid = DateTime.Now;
			CurrentOrder.Active = true;

			try
			{
				foreach (Combo c in CurrentOrder.Combos)
				{
					c.Active = true;

					DataAccess.Combo_Upsert(c);
				}

				DataAccess.Order_Upsert(CurrentOrder);
				PrintManager.OrderSticker(CurrentOrder);

				DialogResult = DialogResult.OK;
			}
			catch (Exception ex)
			{
				Program.LogError(ex, Messages.ErrorCouldNotInsertOrder);
				PrintManager.OrderUpsertFailed(CurrentOrder);

				DialogResult = DialogResult.Retry;
			}
		}

		#endregion
	}
}